package com.qf.dao.UserdaoImpl;

import com.qf.dao.Userdao;
import com.qf.pojo.Profile;
import com.qf.pojo.User;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author Wy
 * @version V1.0
 * @Project web人员管理系统
 * @Package com.qf.dao.UserdaoImpl
 * @Description:
 * @Date 2022/5/27 12:33
 */
public class UserdaoImpl implements Userdao {
    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDateSource());
    @Override
    public User login(String name) {
        String sql="select * from t_user where username=?";
        try {
       return      queryRunner.query(sql,new BeanHandler<User>(User.class),name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        String sql="insert into t_user (username,pssword,status) value(?,?,?)";
        try {
       return      queryRunner.update(sql,user.getUsername(),user.getPassword(),user.getStatus());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Profile> all() {
        String sql="select *from t_profile";
        try {
          return   queryRunner.query(sql,new BeanListHandler<Profile>(Profile.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int delect(Integer id) {
        String sql="delete from t_profile where id=?";
        try {
         return    queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Profile logins(String name) {
        String sql="select *from t_profile where name=?";
        try {
        return     queryRunner.query(sql,new BeanHandler<Profile>(Profile.class),name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
